package com.mtons.mblog.web.controller.admin;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.data.AccountProfile;
import com.mtons.mblog.modules.data.AnswerVO;
import com.mtons.mblog.modules.data.QuestVO;
import com.mtons.mblog.modules.entity.Answer;
import com.mtons.mblog.modules.service.AnswerService;
import com.mtons.mblog.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: mblog
 * @description:
 * @author: Mr.Run
 * @create: 2021-04-11 00:20
 **/
@Controller("adminAnswerController")
@RequestMapping("/admin/answer")
public class AnswerController extends BaseController {

    @Autowired
    private AnswerService answerService;

    @RequestMapping("/list")
    public String list(String content, ModelMap model, HttpServletRequest request) {
        long id = ServletRequestUtils.getLongParameter(request, "id", Consts.ZERO);

        Pageable pageable = wrapPageable(Sort.by(Sort.Direction.DESC, "weight", "created"));
        Page<AnswerVO> page = answerService.paging4Admin(pageable, content);
        model.put("page", page);
        model.put("content", content);
        model.put("id", id);
        return "/admin/answer/list";
    }

    protected PageRequest wrapPageable() {
        return wrapPageable(null);
    }

    protected PageRequest wrapPageable(Sort sort) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        int pageSize = ServletRequestUtils.getIntParameter(request, "pageSize", 10);
        int pageNo = ServletRequestUtils.getIntParameter(request, "pageNo", 1);

        if (null == sort) {
            sort = Sort.unsorted();
        }
        return PageRequest.of(pageNo - 1, pageSize, sort);
    }

    /**
     * 跳转到回答编辑方法
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String toUpdate(Long id, ModelMap model) {
        String editor = siteOptions.getValue("editor");
        if (null != id && id > 0) {
            AnswerVO view = answerService.get(id);
            if (StringUtils.isNoneBlank(view.getEditor())) {
                editor = view.getEditor();
            }
            model.put("view", view);
        }
        model.put("editor", editor);
        return "/admin/answer/view";
    }

    /**
     * 更新问题方法
     * @author LBB
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String subUpdate(AnswerVO answer) {
        if (answer != null) {
            if (answer.getId() > 0) {
                answerService.update(answer);
            } else {
                AccountProfile profile = getProfile();
                answer.setAuthorId(profile.getId());
                answerService.post(answer);
            }
        }
        return "redirect:/admin/answer/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam("id") List<Long> id) {
        Result data = Result.failure("操作失败");
        if (id != null) {
            try {
                answerService.delete(id);
                data = Result.success();
            } catch (Exception e) {
                data = Result.failure(e.getMessage());
            }
        }
        return data;
    }

    @RequestMapping("/featured")
    @ResponseBody
    public Result featured(Long id, HttpServletRequest request) {
        Result data = Result.failure("操作失败");
        int featured = ServletRequestUtils.getIntParameter(request, "featured", Consts.FEATURED_ACTIVE);
        if (id != null) {
            try {
                answerService.updateFeatured(id, featured);
                data = Result.success();
            } catch (Exception e) {
                data = Result.failure(e.getMessage());
            }
        }
        return data;
    }

    @RequestMapping("/weight")
    @ResponseBody
    public Result weight(Long id, HttpServletRequest request) {
        Result data = Result.failure("操作失败");
        int weight = ServletRequestUtils.getIntParameter(request, "weight", Consts.FEATURED_ACTIVE);
        if (id != null) {
            try {
                answerService.updateWeight(id, weight);
                data = Result.success();
            } catch (Exception e) {
                data = Result.failure(e.getMessage());
            }
        }
        return data;
    }
}
